ast-grep: Rule Object
from ast-grepのrule
ast-grepのruleのyaml内のrule:の中に書くやつ
これの組み合わせで、より複雑なクエリが書ける
1つのrule:内の
全てのfieldはoptional
全てのfieldのANDで検索される
なのでfieldの順序は関係ない
項目はたかだか11個ぐらいしかない
それを更に3つの種類に分類している
code:yml
rule:
# atomic rule
pattern: 'search.pattern'
kind: 'tree_sitter_node_kind'
regex: 'rust|regex'
# relational rule
inside: { pattern: 'sub.rule' }
has: { kind: 'sub_rule' }
follows: { regex: 'can|use|any' }
precedes: { kind: 'multi_keys', pattern: 'in.sub' }
# composite rule
all: {pattern: 'match.all'}, {kind: 'match_all'}
any: {pattern: 'match.any'}, {kind: 'match_any'}
not: { pattern: 'not.this' }
matches: 'utility-rule'
(けど、versionが上がるとたまに新しく生えていたりする)mrsekut.icon
#wip
f($A)にマッチさせたうえで、$Aだけ返す、とか書けないのかな #??
かけたわ
code:yml
id: APIを呼んでいる箇所を一覧する
language: TypeScript
rule:
kind: template_string
inside:
pattern: f($A)
stopBy: end
と思ったが、こんなことしなくても
$ ast-grep scan --json | jq '.[] | .metaVariables.single.A.text'
みたいにして、結果のjsonに対してjqしたほうが楽だmrsekut.icon
https://ast-grep.github.io/guide/rule-config.html#rule-object
https://ast-grep.github.io/reference/rule.html
https://ast-grep.github.io/guide/rule-config.html
https://zenn.dev/makotot/articles/ea823805582e5c#ルールの詳細
ast-grep: Atomic Rule
ast-grep: Relational Rule
ノードが別のノードに囲まれているかどうかを確認するルール
例えば、「for文の中だけで検索」とかができる
https://ast-grep.github.io/guide/rule-config/relational-rule.html
https://ast-grep.github.io/reference/rule.html#relational-rules
https://zenn.dev/makotot/articles/ea823805582e5c#relationalルール
inside: { pattern: 'sub.rule' }
has: { kind: 'sub_rule' }
follows: { regex: 'can|use|any' }
precedes: { kind: 'multi_keys', pattern: 'in.sub' }
stopBy
https://ast-grep.github.io/guide/rule-config/relational-rule.html#stopby
ast-grep: Composite Rule
https://zenn.dev/makotot/articles/ea823805582e5c#compositeルール
https://ast-grep.github.io/guide/rule-config/composite-rule.html
https://ast-grep.github.io/reference/rule.html#composite-rules
論理演算子を使用してサブルールを結合するルール。
all: [ {pattern: 'match.all'}, {kind: 'match_all'} ]
any: [ {pattern: 'match.any'}, {kind: 'match_any'} ]
not: { pattern: 'not.this' }
matches: 'utility-rule'